package com.steven.concurrency.example;


import com.steven.concurrency.annoations.NotThreadSafe;

import java.util.concurrent.*;

@NotThreadSafe
public class CountDownTest {


    //请求总数
    private static int clientTotal=5000;
    //并发数量
    private static int threadTotal=200;

    public static int count =0;

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Semaphore semaphore = new Semaphore(threadTotal);
        final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
        for (int i = 0; i <clientTotal ; i++) {
            executorService.execute(()->{
                try {
                    semaphore.acquire();
                    add();
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();
        System.out.println(count);
    }

    public static synchronized void  add(){
        count++;
    }
}
